package org.sbt.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.annotation.Resource;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.Hibernate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@Autowired
	private TransportClient client;

	@GetMapping("/")
	public String index() {
		return "index";
	}

	/**
	 * http://127.0.0.1:8081/demo/get/book/novel?id=3
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/get/book/novel")
	@ResponseBody
	public ResponseEntity get(@RequestParam(name = "id", defaultValue = "") String id) {
		GetResponse result = client.prepareGet("book", "novel", id).get();
		if (!result.isExists()) {
			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity<>(result.getSource(), HttpStatus.OK);
	}
	@GetMapping("/update/book/novel")
	@ResponseBody
	public ResponseEntity update(@RequestParam(name = "id", defaultValue = "") String id,
			@RequestParam(name = "title", defaultValue = "") String title) {
		UpdateResponse result=null;
		try {
			UpdateRequest u=new UpdateRequest("book","novel",id);
			XContentBuilder bulder =XContentFactory.jsonBuilder().startObject();
			bulder.field("title", title);
			bulder.field("id", id);
			bulder.endObject();
			u.doc(bulder );
			result = client.update(u).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ResponseEntity<>(result.status(), HttpStatus.OK);
	}
	@GetMapping("/query/book/novel")
	@ResponseBody
	public ResponseEntity query(@RequestParam(name = "id", defaultValue = "") String id,
			@RequestParam(name = "title", defaultValue = "") String title) {
		List list = new ArrayList<>();
		try {
			BoolQueryBuilder query = QueryBuilders.boolQuery();
			query.must(QueryBuilders.matchQuery("title", "ooo"));
			SearchRequestBuilder bu = client.prepareSearch("book").setTypes("novel")
			.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
			.setQuery(query)
			.setFrom(0)
			.setSize(10);
			System.out.println(bu);
			SearchResponse re = bu.get();
			
			for( SearchHit h:re.getHits()){
				System.out.println(h.getSource());
				list.add(h.getSource());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<>(list, HttpStatus.OK);
	}
	
	/**
	 * http://127.0.0.1:8081/demo/delete/book/novel?id=3
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/delete/book/novel")
	@ResponseBody
	public ResponseEntity delete(@RequestParam(name = "id", defaultValue = "") String id) {
		 DeleteResponse result = client.prepareDelete("book", "novel", ""+id).get();
		return new ResponseEntity<>(result.getResult().toString(), HttpStatus.OK);
	}
	
	@RequestMapping("/add/book/novel")
	@ResponseBody
	public ResponseEntity add(@RequestParam(name = "id", defaultValue = "") Integer id,
			@RequestParam(name = "title", defaultValue = "") String title) {
		try {
			XContentBuilder source = XContentFactory.jsonBuilder()
			.startObject()
			.field("title",title)
			.field("id",id)
			.endObject();
			IndexResponse result = client.prepareIndex("book","novel")
			.setSource(source)
			.get();
			return new ResponseEntity<>(result.getId(), HttpStatus.OK);
		} catch (IOException e) {
			e.printStackTrace();
			return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		
	}
}
